﻿using System;
using System.Web.Mvc;
using GdNet.Core.Assistants;
using GdNet.WebFramework.Business.System;
using GdNet.WebFramework.Domain.Repositories.ContentManagement;
using GdNet.WebFramework.Modules.Common.ModuleBase;
using GdNet.WebFramework.Modules.ContentManagement.Dtos;

namespace GdNet.WebFramework.Modules.ContentManagement.Actions
{
    public class AdminDetailContentItemAction : AbstractModuleActionService, IModuleActionPostbackService
    {
        private readonly IContentItemRepository _contentItemRepository;
        private readonly IModelBidingService _modelBidingService;

        public AdminDetailContentItemAction(IContentItemRepository contentItemRepository, IModelBidingService modelBidingService)
        {
            _contentItemRepository = contentItemRepository;
            _modelBidingService = modelBidingService;
        }

        public override object Execute(ActionDetail actionDetail)
        {
            if (GuidAssistant.CanParse(actionDetail.Id) == false)
            {
                return null;
            }

            var item = _contentItemRepository.GetById(new Guid(actionDetail.Id));
            return new ContentItemDto(item);
        }

        public bool Submit(ActionDetail actionDetail, FormCollection formCollection)
        {
            var item = _contentItemRepository.GetById(new Guid(actionDetail.Id));
            _modelBidingService.UpdateModel(item, formCollection);
            return _contentItemRepository.Save(item);
        }
    }
}
